package com.woniu.airent.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.airent.dto.*;
import com.woniu.airent.entity.Extra;
import com.woniu.airent.entity.Items;
import com.woniu.airent.service.*;
import com.woniu.airent.service.ExreaService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/cotenancy")
public class CotenancyController {

    @Autowired
    CotenancyService cotenancyService;

    @Autowired
    CotenancyImgService cotenancyImgService;

    @Autowired
    ItemsService itemsService;

    @Autowired
    ExreaService extraService;

    @Autowired
    MyHouseService myHouseService;


    /**
     * 发布合租信息
     * @param cotenancyDto 合租信息对象
     * @param imgPath  合租信息发布时上传的图片路径
     * @return
     */
    @ResponseBody
    @RequestMapping("/addPost")
    public String addPost(CotenancyDto cotenancyDto,@RequestParam("imgPath[]") String imgPath[]){
        String houseNumber = cotenancyDto.getHouseNumber();
        String prefix = "https://airent-1303816068.cos.ap-shanghai.myqcloud.com/";
        CotenancyDto c = cotenancyService.findCotenancyByHouseNumber(houseNumber);
        if (c == null){
            for (int i = 0; i < imgPath.length; i++) {
                String path = prefix + imgPath[i];
                Integer add = cotenancyImgService.addCotanancyImg(houseNumber,path);
            }
            String cover = prefix + imgPath[0];
            cotenancyDto.setCover(cover);
            Integer i = cotenancyService.addCotenancy(cotenancyDto);
            return "1";
        }else {
            return "2";
        }
    }

    @ResponseBody
    @RequestMapping("/showMyCotenancy")
    public PageInfo showMyCotenancy(String keyWord,Integer minPrice,Integer maxPrice,
                                    @RequestParam(value = "sort",defaultValue = "DESC") String sort,
                                    @RequestParam(value = "roommateGender",defaultValue = "") String gender,
                                    @RequestParam(value = "now",defaultValue = "1") Integer now,
                                    @RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize){
        PageInfo pageInfo = cotenancyService.findCotenancy(keyWord,minPrice,maxPrice,gender,now,pageSize,sort);
        return pageInfo;
    }

    @RequestMapping(value = "/showOneHouse/{houseNumber}",method = RequestMethod.GET)
    public String showOneHouse(@PathVariable String houseNumber, Model model){
        CotenancyDto cotenancyDto = cotenancyService.findCotenancyByHouseNumber(houseNumber);
        List<CotenancyImgDto> cotenancyImgDto = cotenancyImgService.findImgByHouseNumber(houseNumber);
        List<Items> items = itemsService.findMyItems(houseNumber);
        List<Extra> extra = extraService.findMyExtra(houseNumber);
        model.addAttribute("house",cotenancyDto);
        model.addAttribute("img",cotenancyImgDto);
        model.addAttribute("item",items);
        model.addAttribute("extra",extra);
        model.addAttribute("houseNumber",houseNumber);
        return "view/roomatedesc";
    }

    @RequestMapping("/collect")
    @ResponseBody
    public String collectionHouse(HttpServletRequest request,
                                  @RequestParam(value = "houseId") Integer houseId){
        UserDto user = (UserDto) request.getSession().getAttribute("user");
        Integer userId = user.getUserId();
        Integer collection = myHouseService.findHouseIsCollection(userId,houseId);
        if (collection == null){
            myHouseService.addCollect(userId,houseId);
            return "1";
        }else {
            myHouseService.removeMyCollection(userId,houseId);
            return "0";
        }
    }
}
